<template>
  <div class="">
    <div class="filter-container">
      <el-select v-model="listQuery.type" style="width: 200px" class="filter-item" placeholder="请选择类型">
        <el-option :label="'全部类型'" :value="''" />
        <el-option v-for="name in types" :key="name" :label="name" :value="name"/>
      </el-select>
      <el-button v-permission="['dictionary:list']" class="filter-item" type="primary" icon="el-icon-search" @click="search">查找</el-button>
      <el-button v-permission="['dictionary:add']" class="filter-item" type="primary" icon="el-icon-edit" @click="showAdd">添加</el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" size="small" element-loading-text="正在查询中..." border fit highlight-current-row>
      <el-table-column align="center" label="ID" prop="id" />
      <el-table-column align="center" label="类型" prop="type"/>
      <el-table-column align="center" label="名称" prop="name" />
      <el-table-column align="center" label="值" prop="value" />
      <el-table-column align="center" label="图标">
        <template slot-scope="scope">
          <el-popover v-if="scope.row.url" placement="right" trigger="hover">
            <img :src="scope.row.url" height="120" width="120">
            <img slot="reference" :src="scope.row.url" height="60" >
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" prop="enableName"/>
      <el-table-column align="center" label="描述" prop="remark"/>
      <el-table-column align="center" label="排序" prop="seq"/>
      <el-table-column align="center" label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-permission="['dictionary:detail']" type="primary" size="mini" @click="showEdit(scope.row)">编辑</el-button>
          <el-button v-permission="['dictionary:delete']" type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-permission="['dictionary:list']" v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="initTable"/>

    <el-dialog :close-on-click-modal="false" :title="isAdd?'新增基础数据':'编辑基础数据'" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="center" label-width="100px">
        <el-form-item label="类型" prop="type">
          <el-input v-model="dataForm.type" auto-complete="off"/>
        </el-form-item>
        <div class="clearfix"/>
        <el-form-item label="名称" prop="name" style="float:left;">
          <el-input v-model="dataForm.name" auto-complete="off"/>
        </el-form-item>
        <el-form-item label="值" prop="value" style="float:left;">
          <el-input v-model="dataForm.value" auto-complete="off" />
        </el-form-item>
        <el-form-item label="排序" prop="seq" style="float:left;">
          <el-input v-model="dataForm.seq" auto-complete="off"/>
        </el-form-item>
        <div class="clearfix"/>
        <el-form-item label="启用" style="float:left;">
          <el-switch v-model="dataForm.enabled" active-color="#13ce66" inactive-color="#dcdfe6"/>
        </el-form-item>
        <el-form-item label="图标" style="float:left;">
          <uploader :file-list.sync="fileList" @success="uploadWaterMarkImgSuccess" @remove="removeWaterMarkImgSuccess"/>
        </el-form-item>
        <div class="clearfix"/>
        <el-form-item label="说明" prop="remark">
          <el-input v-model="dataForm.remark" auto-complete="off" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-permission="['dictionary:add']" v-if="isAdd" type="primary" @click="add">确定</el-button>
        <el-button v-permission="['dictionary:edit']" v-else type="primary" @click="edit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { DictionaryService } from '@/api/CommonService'
import { uploadPath } from '@/api/CommonService'
import Pagination from '@/components/Pagination'
import Uploader from '@/components/Uploader'
import Notify from '@/utils/notify'

export default {
  name: 'Dictionary',
  components: { Pagination, Uploader },
  data() {
    return {
      fileList: [],
      uploadPath, list: null, total: 0, listLoading: true, types: [],
      listQuery: { pageNo: 1, pageSize: 20, userName: undefined },
      dataForm: { id: undefined, name: undefined, value: undefined, type: undefined, seq: 0, icon: undefined, remark: undefined, enabled: true },
      dialogFormVisible: false, isAdd: true,
      rules: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        value: [{ required: true, message: '值不能为空', trigger: 'blur' }],
        type: [{ required: true, message: '类型不能为空', trigger: 'blur' }],
        seq: [{ required: true, message: '排序不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.initTable()
    this.loadTypes()
  },
  methods: {
    initTable() {
      this.listLoading = true
      DictionaryService.listDictionary(this.listQuery).then(result => {
        this.list = result.data
        this.total = result.totalCount
        this.listLoading = false
      })
    },
    uploadWaterMarkImgSuccess(data) {
      this.dataForm.icon = data.path
    },
    removeWaterMarkImgSuccess() {
      this.dataForm.icon = undefined
    },
    loadTypes() {
      DictionaryService.listTypes().then(result => {
        this.types = result
      })
    },
    search() {
      this.listQuery.pageNo = 1
      this.initTable()
    },
    resetForm() {
      this.dataForm = { id: undefined, name: undefined, value: undefined, type: undefined, icon: undefined, remark: undefined, seq: 0, enabled: true }
    },
    showAdd() {
      this.isAdd = true
      this.fileList = []
      this.resetForm()
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    add() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          DictionaryService.addDictionary(this.dataForm)
            .then(() => {
              this.dialogFormVisible = false
              Notify.success(this, '添加成功!')
              this.initTable()
            })
        }
      })
    },
    showEdit(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogFormVisible = true
      this.isAdd = false
      if (this.dataForm.icon) {
        const url = this.dataForm.url
        this.fileList = [{ name: url.substring(url.lastIndexOf('/')), url: url }]
      } else {
        this.fileList = []
      }
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    edit() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          DictionaryService.editDictionary(this.dataForm)
            .then(() => {
              this.dialogFormVisible = false
              Notify.success(this, '更新成功!')
              this.initTable()
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('您确认要删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        DictionaryService.deleteDictionary(row.id)
          .then(() => {
            Notify.success(this, '删除成功!')
            this.initTable()
          })
      })
    }
  }
}
</script>
